package service.impl;

import global.Constants;
import global.security.SessionUtils;

import java.sql.Timestamp;
import java.util.List;

import service.AccountCategoryService;
import dao.AccountCategoryDAO;
import domain.AccountCategory;

/**
 * 账目分类Service实现类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class AccountCategoryServiceImpl implements AccountCategoryService {

	/**
	 * 账目分类Dao
	 * */
	private AccountCategoryDAO accountCategoryDao = null;

	/**
	 * 设定账目分类Dao
	 * */
	public void setAccountCategoryDao(AccountCategoryDAO accountCategoryDao) {
		this.accountCategoryDao = accountCategoryDao;
	}
	
	/**
	 * 查询账目分类数量
	 * @param paramAccountCategory 账目分类Domain
	 * @return int 账目分类数量
	 */
	public int countByCriteria(AccountCategory paramAccountCategory) {
		paramAccountCategory = getConditions(paramAccountCategory);
		int count = accountCategoryDao.countByCriteria(paramAccountCategory);
		return count;
	}

	/**
	 * 查询账目分类列表不带分页
	 * @param paramAccountCategory 账目分类Domain
	 * @return List 账目分类列表
	 */
	public List<AccountCategory> selectByCriteria(AccountCategory paramAccountCategory) {
		// 查询账目分类列表不带分页
		paramAccountCategory = getConditions(paramAccountCategory);
		List<AccountCategory> accountCategoryList = accountCategoryDao.selectByCriteria(paramAccountCategory);
		if (accountCategoryList != null && accountCategoryList.size() != 0) {
			return accountCategoryList;
		} else {
			return null;
		}
	}

	/**
	 * 查询账目分类列表带分页
	 * @param paramAccountCategory 账目分类Domain
	 * @return List 账目分类列表
	 */
	public List<AccountCategory> selectByCriteriaForPaging(AccountCategory paramAccountCategory) {
		// 查询账目分类列表带分页
		paramAccountCategory = getConditions(paramAccountCategory);
		List<AccountCategory> accountCategoryList = accountCategoryDao.selectByCriteriaForPaging(paramAccountCategory, paramAccountCategory.getStart(), paramAccountCategory.getLimit());
		if (accountCategoryList != null && accountCategoryList.size() != 0) {
			return accountCategoryList;
		} else {
			return null;
		}
	}
	
	/**
	 * 添加账目分类
	 * @param accountCategory 账目分类Domain
	 */
	public void insert(AccountCategory accountCategory) {
		// 删除区分：未删除
		accountCategory.setDelflag(Constants.DEL_FLAG_1);
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		accountCategory.setCreateUser(SessionUtils.getCurrentUserId());
		accountCategory.setCreateTime(sysdate);
		accountCategory.setUpdateUser(SessionUtils.getCurrentUserId());
		accountCategory.setUpdateTime(sysdate);
		accountCategoryDao.insert(accountCategory);
	}
	
	
	/**
	 * 修改账目分类
	 * @param accountCategory 账目分类Domain
	 */
	public void update(AccountCategory accountCategory) {
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		accountCategory.setUpdateUser(SessionUtils.getCurrentUserId());
		accountCategory.setUpdateTime(sysdate);
		accountCategoryDao.updateByPrimaryKey(accountCategory);
	}
	
	
	/**
	 * 删除账目分类
	 * @param accountCategoryIdStr 账目分类ID
	 * @param rowNumStr 行号
	 */
	public void delete(String accountCategoryIdStr) {
		String[] categoryIds = accountCategoryIdStr.split(",");
		for (int i = 0; i < categoryIds.length; i++) {
			int categroyId = Integer.parseInt(categoryIds[i]);
			// 删除收支项目
			accountCategoryDao.deleteByPrimaryKey(categroyId);
		}
	}
	
	/**
	 * 设置公共参数
	 * @param paramAccountCategory 参数对象
	 */
	private AccountCategory getConditions(AccountCategory paramAccountCategory){
		paramAccountCategory.setCreateUser(SessionUtils.getCurrentUserId());// 当前用户
		return paramAccountCategory;
	}
}
